package org.rabbitmq.rabbitextensions.config;

import org.rabbitmq.rabbitextensions.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    //声明队列:
    @Bean("ackQueue")
    public Queue ackQueue(){
                             //队列是否持久化:持久化
        return QueueBuilder.durable(Constants.ACK_QUEUE).build();
    }
    //声明交换机
    @Bean("ackExchange")
    public DirectExchange ackExchange(){                             //交换机是否持久化:
        return ExchangeBuilder.directExchange(Constants.ACK_EXCHANGE).durable(true).build();
    }
    //队列和交换机绑定
    @Bean("bindingQueue")
    public Binding bindingQueue(@Qualifier("ackExchange") DirectExchange directExchange, @Qualifier("ackQueue") Queue queue){
        return BindingBuilder.bind(queue).to(directExchange).with("ack");
    }

    //声明非持久化队列:
    @Bean("presQueue")
    public Queue presQueue(){
        //队列是否持久化:
        return QueueBuilder.durable(Constants.PRES_QUEUE).build();
//        return QueueBuilder.nonDurable(Constants.PRES_QUEUE).build();
    }
    //声明非持久化交换机
    @Bean("presExchange")
    public Exchange presExchange(){                                    //非持久化
        return ExchangeBuilder.directExchange(Constants.PRES_EXCHANGE).durable(true).build();
    }

    //交换机和队列绑定
    @Bean("bindingpresQueue")
    public Binding bindingprexQueue(@Qualifier("presExchange") Exchange exchange,@Qualifier("presQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("pres").noargs();
    }

    //发送确认
    //声明队列
    @Bean("confirmQueue")
    public Queue confirmQueue(){
        return QueueBuilder.durable(Constants.CONFIRM_QUEUE).build();
    }
    //声明交换机
    @Bean("confirmExchange")
    public Exchange confirmExchange(){
        return ExchangeBuilder.directExchange(Constants.CONFIRM_EXCHANGE).durable(true).build();
    }
    //交换机和队列绑定
    @Bean("bindingConfirm")
    public Binding bindingConfirm( @Qualifier("confirmExchange") Exchange exchange,@Qualifier("confirmQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("confirm").noargs();
    }
    //重试机制
    @Bean("retryQueue")
    public Queue retryQueue(){
        return QueueBuilder.durable(Constants.RETRY_QUEUE).build();
    }

    @Bean("retryExchange")
    public Exchange retryExchange(){
        return ExchangeBuilder.directExchange(Constants.RETRY_EXCHANGE).durable(true).build();
    }
    @Bean("bindingretry")
    public Binding bindingretry(@Qualifier("retryExchange") Exchange exchange,@Qualifier("retryQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("retry").noargs();
    }
    //设置过期时间ttl:
    @Bean("ttlQueue")
    public Queue ttlQueue(){
        return QueueBuilder.durable(Constants.TTL_QUEUE).build();
    }
    @Bean("ttlExchange")
    public Exchange ttlExchange(){
        return ExchangeBuilder.directExchange(Constants.TTL_EXCHANGE).build();
    }
    @Bean("bindingTtl")
    public Binding bindingTtl(@Qualifier("ttlExchange") Exchange exchange,@Qualifier("ttlQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("ttl").noargs();
    }

    //设置队列的过期时间
    @Bean("ttlQueue2")
    public Queue ttlQueue2(){                           //设置队列的过期时间30秒
        return QueueBuilder.durable(Constants.TTL_QUEUE2).ttl(30000).build();
    }
    //绑定关系,还是和原来的交换机绑定
    @Bean("bindingTtl2")
    public Binding bindingTtl2(@Qualifier("ttlExchange") Exchange exchange,@Qualifier("ttlQueue2") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("ttl").noargs();
    }
    //事务队列
    @Bean("transQueue")
    public Queue transQueue(){
        return QueueBuilder.durable(Constants.TRANS_QUEUE).build();
    }
    @Bean("qosQueue")
    public Queue qosQueue(){
        return QueueBuilder.durable(Constants.QOS_QUEUE).build();
    }

    @Bean("qosExchange")
    public Exchange qosExchange(){
        return ExchangeBuilder.directExchange(Constants.QOS_EXCHANGE).build();
    }
    @Bean("qosBinding")
    public Binding qosBinding(@Qualifier("qosQueue") Queue queue, @Qualifier("qosExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("qos").noargs();
    }

}
